﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ChartEdit
{
    public class SongSection : List<SongSectionEntry>
    {
        public const string SectionName = "Song";
        

        public bool ContainsKey( string key )
        {
            foreach ( SongSectionEntry entry in this )
            {
                if ( entry.Key == key )
                    return true;
            }
            return false;
        }

        public SongSectionEntry this[ string key ]
        {
            get
            {
                foreach ( SongSectionEntry entry in this )
                {
                    if ( entry.Key == key )
                        return entry;
                }
                return null;
            }
        }
    }

    public class SongSectionEntry
    {
        static readonly Regex SongRegex = new Regex( "(?<key>.+)\\s*\\=\\s*(?<value>.+)", RegexOptions.Compiled | RegexOptions.IgnorePatternWhitespace | RegexOptions.Singleline );

        
        public string Key { get; set; }
        public string Value { get; set; }


        public static SongSectionEntry Parse( string entryStr )
        {
            Match match = SongRegex.Match( entryStr );

            if ( !match.Success )
                return null;

            SongSectionEntry entry = new SongSectionEntry();

            string key = match.Groups[ "key" ].Value.Trim();
            string value = match.Groups[ "value" ].Value.Trim().Trim( "\"".ToCharArray() );

            entry.Key = key;
            entry.Value = value;

            return entry;
        }
    }
}
